﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JPGResizer.Pages
{
    /// <summary>
    /// Interaction logic for Page3.xaml
    /// </summary>
    public partial class Page3 : Page
    {
        public Page3()
        {
            InitializeComponent();
            Bind_Events();
        }

        private void Bind_Events()
        {
            this.Loaded += (o, e) =>
                {
                    Bind_Datas();
                    Flush_Pre();
                };
            this.rbNameType1.Checked += (o, e) =>
                {
                    Helper.ValueHelper.CurrentValues.NameType = Helper.NameType.NameType1;
                    Flush_Pre();
                };
            this.rbNameType2.Checked += (o, e) =>
                {
                    Helper.ValueHelper.CurrentValues.NameType = Helper.NameType.NameType2;
                    Flush_Pre();
                };
            this.tbStartNum.TextChanged += (o, e) =>
                {
                    try
                    {
                        Helper.ValueHelper.CurrentValues.StartNum = Convert.ToInt32(this.tbStartNum.Text.Trim());
                        Flush_Pre();
                    }
                    catch { }
                };
            this.tbFirstExtenssion.TextChanged += (o, e) =>
                {
                    Helper.ValueHelper.CurrentValues.FirstExtenssion = this.tbFirstExtenssion.Text.Trim();
                    Flush_Pre();
                };
            this.tbLastExtenssion.TextChanged += (o, e) =>
                {
                    Helper.ValueHelper.CurrentValues.LastExtenssion = this.tbLastExtenssion.Text.Trim();
                    Flush_Pre();
                };
            this.tbSavePath.TextChanged += (o, e) =>
                {
                    Helper.ValueHelper.CurrentValues.SavePath = this.tbSavePath.Text.Trim();
                };
        }

        private void SelectFolder_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbDialog = new System.Windows.Forms.FolderBrowserDialog();

            if (fbDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.tbSavePath.Text = fbDialog.SelectedPath;
            }
        }

        private void Flush_Pre()
        {
            string center = string.Empty;
            switch (Helper.ValueHelper.CurrentValues.NameType)
            {
                case Helper.NameType.NameType1:
                    {
                        center = "原始文件名";
                        break;
                    }
                case Helper.NameType.NameType2:
                    {
                        center = Helper.ValueHelper.CurrentValues.StartNum.ToString();
                        break;
                    }
            }
            this.txtPre.Text = string.Format("{0}{1}{2}.jpg", 
                                             Helper.ValueHelper.CurrentValues.FirstExtenssion,
                                             center,
                                             Helper.ValueHelper.CurrentValues.LastExtenssion);
        }

        private void Bind_Datas()
        {
            switch (Helper.ValueHelper.CurrentValues.NameType)
            {
                case Helper.NameType.NameType1:
                    {
                        this.rbNameType1.IsChecked = true;
                        break;
                    }
                case Helper.NameType.NameType2:
                    {
                        this.rbNameType2.IsChecked = true;
                        break;
                    }
            }
            this.tbStartNum.Text = Helper.ValueHelper.CurrentValues.StartNum.ToString();
            this.tbFirstExtenssion.Text = Helper.ValueHelper.CurrentValues.FirstExtenssion;
            this.tbLastExtenssion.Text = Helper.ValueHelper.CurrentValues.LastExtenssion;
            this.tbSavePath.Text = Helper.ValueHelper.CurrentValues.SavePath;

        }
    }
}
